package com.sunny.ihealth.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunny.ihealth.R;
import com.sunny.ihealth.entity.bmob.Post;
import com.sunny.ihealth.entity.bmob.State;
import com.sunny.ihealth.ui.activity.PhotoViewActivity;
import com.sunny.ihealth.utils.TimeUtils;
import com.sunny.ninepictureview.NinePictureView;

import java.util.List;
import java.util.Locale;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 帖子的适配器
 * Created by Sunny  An on 2016/5/30.
 */
public class ZoneAdapter extends RecyclerView.Adapter<ZoneAdapter.Holder> implements View.OnClickListener {

    private Context context;
    private List<Post> data;
    private LayoutInflater inflater;

    private OnViewClickListener listener;

    public ZoneAdapter(Context context, List<Post> data) {
        this.context = context;
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_post, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final Post post = data.get(position);
        if (post.getAuthor().hasHead()) {
            Picasso.with(context).load(post.getAuthor().getHeadFile().getUrl()).into(holder.ivHead);
        }
        holder.tvAuthor.setText(post.getAuthor().getNickname());
        holder.tvTime.setText(TimeUtils.getFriendlyTime(post.getCreatedAt()));
        holder.tvContent.setText(post.getContent());
        if (TextUtils.isEmpty(post.getContent())) {
            holder.tvContent.setVisibility(View.GONE);
        } else {
            holder.tvContent.setVisibility(View.VISIBLE);
        }

        //NinePictureView
        if (post.getPictures() != null && post.getPictures().size() != 0) {
            holder.ninePictureView.setVisibility(View.VISIBLE);
            PostNinePicAdapter adapter = new PostNinePicAdapter(context, post.getPictures());
            holder.ninePictureView.setAdapter(adapter);
        } else {
            holder.ninePictureView.setVisibility(View.GONE);
        }

        String likeStr;
        if (post.isLiked()) {
            likeStr = String.format(Locale.getDefault(), "已赞(%d)", post.getLikeNumber());
        } else {
            likeStr = String.format(Locale.getDefault(), "赞(%d)", post.getLikeNumber());
        }
        holder.btnLike.setText(likeStr);

        String commentStr = String.format(Locale.getDefault(), "评论(%d)", post.getCommentNumber());
        holder.btnComment.setText(commentStr);

        int state = post.getAuthor().isFriend();
        if (state == State.STATE_BOTH_FRIEND) {
            holder.tvAddFriend.setText("互相关注");
            holder.tvAddFriend.setTextColor(context.getResources().getColor(R.color.lightGray));
        } else if (state == State.STATE_ADD) {
            holder.tvAddFriend.setText("已关注");
            holder.tvAddFriend.setTextColor(context.getResources().getColor(R.color.lightGray));
        } else {
            holder.tvAddFriend.setText("+关注");
            holder.tvAddFriend.setTextColor(context.getResources().getColor(R.color.addFriendBlue));
        }

        holder.btnLike.setOnClickListener(this);
        holder.btnComment.setOnClickListener(this);
        holder.llItem.setOnClickListener(this);
        holder.tvAddFriend.setOnClickListener(this);
        holder.ninePictureView.setOnItemClickListener(new NinePictureView.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(context, PhotoViewActivity.class);
                intent.putExtra("imgs", post.getListPictures());
                intent.putExtra("position", position);
                context.startActivity(intent);
            }
        });
        holder.setTag(position);

    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public void onClick(View v) {
        if (listener == null) {
            return;
        }
        int position = (int) v.getTag();
        listener.onViewClick(v.getId(), position);
    }


    public OnViewClickListener getListener() {
        return listener;
    }

    public void setListener(OnViewClickListener listener) {
        this.listener = listener;
    }

    /**
     * 设置喜欢按钮的喜欢数量
     *
     * @param increase 加一为true  减一为false
     * @param position 按钮的位置
     */
    public void setLikeBtnText(boolean increase, int position) {
        int likeNumber = data.get(position).getLikeNumber();
        if (increase) {
            data.get(position).setLikeNumber(likeNumber + 1);
            data.get(position).setLiked(true);
        } else {
            data.get(position).setLikeNumber(likeNumber <= 0 ? 0 : likeNumber - 1);
            data.get(position).setLiked(false);
        }
        notifyItemChanged(position, R.id.btn_post_like);
    }


    /**
     * 设置添加好友，改变数据所有结果
     *
     * @param isFriend 是否是好友
     * @param position 按钮的位置
     */
    public void setIsFriend(Integer isFriend, int position) {
        String objectId = data.get(position).getAuthor().getObjectId();
        for (int i = 0; i < data.size(); i++) {
            Post post = data.get(i);
            if (post.getAuthor().getObjectId().equals(objectId)) {
                post.getAuthor().setIsFriend(isFriend);
                notifyItemChanged(i, R.id.tv_post_addFriend);
            }
        }
    }


    static class Holder extends RecyclerView.ViewHolder {

        LinearLayout llItem;
        CircleImageView ivHead;
        TextView tvAuthor, tvTime, tvContent;
        NinePictureView ninePictureView;
        Button btnLike, btnComment;
        TextView tvAddFriend;


        public Holder(View v) {
            super(v);
            llItem = (LinearLayout) v.findViewById(R.id.ll_post_item);
            ivHead = (CircleImageView) v.findViewById(R.id.image_post_head);
            tvAuthor = (TextView) v.findViewById(R.id.tv_post_author);
            tvTime = (TextView) v.findViewById(R.id.tv_post_time);
            tvContent = (TextView) v.findViewById(R.id.tv_post_content);
            ninePictureView = (NinePictureView) v.findViewById(R.id.ninePicView_post);
            btnLike = (Button) v.findViewById(R.id.btn_post_like);
            btnComment = (Button) v.findViewById(R.id.btn_post_comment);
            tvAddFriend = (TextView) v.findViewById(R.id.tv_post_addFriend);
        }


        public void setTag(Object tag) {
            llItem.setTag(tag);
            btnComment.setTag(tag);
            btnLike.setTag(tag);
            tvAddFriend.setTag(tag);
            ivHead.setTag(tag);
            tvAuthor.setTag(tag);
        }
    }


}
